import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { LoadingController } from '@ionic/angular';

@Injectable()
export class HttpService {
    defaultHeaders: HttpHeaders = new HttpHeaders({
        'Content-Type': 'application/json',
    });

    loading;

    constructor(private http: HttpClient, public loadingCtrl: LoadingController) { }
    /**
     * @param {string} url地址 description
     * @param {any} [options] 可选提交的参数
     * @param {any} [header] 可选设置的头信息
     * @memberof HttpService description
     * @title: 封装一个get请求的基础类
     */
    async presentLoading() {
        this.loading = await this.loadingCtrl.create({
            message: '加载中...',
            spinner: 'bubbles'
        });
        return await this.loading.present();
    }


    getData(url: string, options?: any, myheaders?: any, isloading?: boolean): Observable<any> {

        // 配置请求头
        const myHeaders: HttpHeaders = new HttpHeaders(myheaders);
        // tslint:disable-next-line:forin
        url += (url.indexOf('?') < 0 ? '?' : '&') + this.param(options);

        if (isloading === true) {
            return new Observable(responseObserver => {
                this.presentLoading().then(() => {
                    this.http.get(url, { headers: myHeaders }).pipe(
                        function (val) {
                            this.loading.dismiss();
                            return val;
                        }
                    );
                });
            });
        } else {
            return this.http.get(url, { headers: myHeaders }).pipe(
                function (val) {
                    return val;
                }
            );
        }


        // return this.http.get(url, { headers: myHeaders }).pipe(
        //     function (val) {
        //         if (isloading === true) {
        //             this.loading.dismiss();
        //         }
        //         return val;
        //     }
        // );
    }

    /**
     * @param url地址 description
     * @param options提交的数据 description
     * @param myheaders可选参数设置头 description
     * @title:封装一个post请求数据的
     */
    postData(url: string, options: any, myheaders?: any, isloading?: boolean): Observable<any> {
        // const loading = this.loadingCtrl.create({
        //     spinner: 'bubbles',
        //     message: '加载中...'
        // });
        // if (isloading === true) {
        //     loading.present();
        // }
        const myHeaders: HttpHeaders = new HttpHeaders(myheaders);
        let params = '';
        if (myheaders['Content-Type'] === 'application/x-www-form-urlencoded') {
            params = this.param(options);
        } else {
            params = options;
        }

        if (isloading === true) {
            return new Observable(responseObserver => {
                this.presentLoading().then(() => {
                    this.http.post(url, params, { headers: myHeaders }).pipe(
                        function (val) {
                            this.loading.dismiss();
                            return val;
                        }
                    );
                });
            });
        } else {
            return this.http.post(url, params, { headers: myHeaders }).pipe(
                function (val) {
                    return val;
                }
            );
        }

        // return this.http.post(url, params, { headers: myHeaders }).pipe(
        //     function (val) {
        //         if (isloading === true) {
        //             this.loading.dismiss();
        //         }
        //         return val;
        //     }
        // );
    }

    /**
     * @param [any] data
     * @returns [string]
     * @title:封装一个序列化get请求的参数的方法
     */
    param(data): string {
        let url = '';
        // tslint:disable-next-line:forin
        for (const k in data) {
            const value = data[k] !== undefined ? data[k] : '';
            url += `&${k}=${encodeURIComponent(value)}`;
        }
        return url ? url.substring(1) : '';
    }
}
